#include <iostream>
#include <vector>
using namespace std;

int min_weight_recur(vector<int> &o, vector<int> &d, vector<int> &w, int n, int oxy, int nit)
{
        if(oxy <= 0 && nit <= 0){
                return 0;
        }
        if(n == 0){
                return INT32_MAX;
        }

        int min_weight = min_weight_recur(o, d, w, n - 1, oxy, nit);
        int min_weight_sel = min_weight_recur(o, d, w, n - 1, oxy - o[n], nit - d[n]);
        if(min_weight_sel != INT32_MAX){
                min_weight = min(min_weight, min_weight_sel + w[n]);
        }

        return min_weight;

}

int min_weight_dp(vector<int> &o, vector<int> &d, vector<int> &w, int n, int oxy, int nit)
{
        vector<vector<int>> dp(oxy + 1, vector<int>(nit + 1, INT32_MAX));
        dp[0][0] = 0;

        for(int i = 1; i <= n; i++){
                for(int j = oxy; j >= 0; j--){
                        for(int k = nit; k >= 0; k--){
                                int lefto = j - o[i];
                                int leftn = k - d[i];
                                if(lefto < 0){
                                        lefto = 0;
                                }
                                if(leftn < 0){
                                        leftn = 0;
                                }
                                if(dp[lefto][leftn] != INT32_MAX){
                                        dp[j][k] = min(dp[j][k], dp[lefto][leftn] + w[i]);
                                }
                        }
                }
        }

        return dp.back().back();
}

int main()
{
        int oxygen, nitrogen, n;
        cin>>oxygen>>nitrogen>>n;
        vector<int> oxy(n + 1, 0), nit(oxy), w(oxy);
        for(int i = 1; i <= n; i++){
                cin>>oxy[i]>>nit[i]>>w[i];
        }

        cout<<min_weight_dp(oxy, nit, w, n, oxygen, nitrogen)<<endl;

        return 0;
}